package com.itcast.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcast.constant.MessageConstant;
import com.itcast.entity.PageResult;
import com.itcast.entity.QueryPageBean;
import com.itcast.entity.Result;
import com.itcast.pojo.CheckGroup;
import com.itcast.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查组管理
 */
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;
    //添加检查组
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try {
            //添加成功
            checkGroupService.add(checkGroup,checkitemIds);
        }catch (Exception e){
            e.printStackTrace();
            //添加失败
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }
    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        return pageResult;
    }
    //根据id查询检查组
    @RequestMapping("/findById")
    public Result findById(Integer id){
        //查询成功
        CheckGroup checkGroup = checkGroupService.findById(id);
        if (checkGroup != null){
            Result result = new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS);
            result.setData(checkGroup);
            return result;
        }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }
    //根据id查询检查项
    @RequestMapping("/findCheckItemByGroup")
    public Result findCheckItemByGroup(Integer id){
        try {
            List<Integer> checkItemId = checkGroupService.findCheckItemByGroup(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkItemId);
        }catch (Exception e){
            e.printStackTrace();
            //查询失败
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
    //编辑检查组和检查项
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try {
            //添加成功
            checkGroupService.edit(checkGroup,checkitemIds);
        }catch (Exception e){
            e.printStackTrace();
            //添加失败
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }
    //删除
    @RequestMapping("/deleteById")
    public Result deleteById(Integer checkgroupId){
        try {
            //添加成功
            checkGroupService.deleteById(checkgroupId);
        }catch (Exception e){
            e.printStackTrace();
            //添加失败
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }
    //查询所有，用于回显新增套餐的复选框
    @RequestMapping("/findAll")
    public Result findAll(){

            List<CheckGroup> checkGroups = checkGroupService.findAll();
            //System.out.println(checkGroups);
            if (checkGroups !=null && checkGroups.size()>0){
                Result result = new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS);
                result.setData(checkGroups);
                return result;
            }
            return new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
    }
}
